package net.zjitc.list;

import net.zjitc.domain.Student;
import net.zjitc.link.Node;

public class MyLinkedList<T> implements MyList<T> {
    private int size;
    private Node<T> first;
    private Node<T> last;

    public MyLinkedList() {
    }

    /**
     * 默认到最后
     *
     * @param data
     * @return
     */
    @Override
    public boolean add(T data) { //30  70
        linkLast(data);
        return true;
    }

    private void linkLast(T data) {
        Node<T> node = new Node<>(data);
        if (first == null) {
            first = node;
            last = first;
        } else {
            last.next = node;
            last = last.next;
        }
        size++;
    }

    /**
     * 插入的任意位置
     *
     * @param index
     * @param data
     * @return
     */
    @Override
    public boolean add(int index, T data) {
        checkPositionIndex(index);
        Node<T> newNode = new Node<>(data);
        if (index == size) { //size==0  加一个判断
            linkLast(data);
            return true;
        }
        if (index == 0) {
            //最前 first
            newNode.next = first;
            first = newNode;
            size++;
            return true;
        }
        //中间
        Node<T> p = first;
        for (int i = 0; i < index - 1; i++) {
            p = p.next;
        }
        newNode.next = p.next;
        p.next = newNode;
        size++;
        return true;
    }

    private void checkPositionIndex(int index) {
        if (index < 0 || index > size) {
            throw new RuntimeException("插入的位置越界");
        }
    }

    @Override
    public T remove(T data) { //data==null  data.equals(..) //1.ArrayList  LinkedList---->优缺点
        //1.查找是否有此数据
        //如果有删除，没有返回null
        Node<T> p = first;
        if (data == null){
            while (p!=null){
               if (p.data == null){
                   //p..删除
               }
            }
        }else {
            while (p!=null){
                if (data.equals(p.data)){
                    //p..删除
                }
            }
        }

        while (p!=null){
            //p.data    data
            //p.data.equals(data);
           // data.equals(p.data)
        }
        return null;
    }

    @Override
    public T remove(int index) {
        checkElementIndex(index);
        Node<T> p = first;
        if (index == 0) {
            first = p.next;
            p.next = null;
            size--;
            return p.data;
        }

        //剩下的怎么处理？删除的如果是最后一个怎么办？ last
        for (int i = 0; i < index - 1; i++) {
            p = p.next;
        }
        T oldData = (T) p.next.data;
        p.next = p.next.next;
        if (index == size - 1) {//删除最后一个时，last发生变化
            last = p;
        }
        size--;
        return oldData;
    }

    @Override
    public boolean set(int index, T data) {
        checkElementIndex(index);
        Node<T> p = first;
        for (int i = 0; i < index; i++) {
            p = p.next;
        }

        p.data = data;
        return true;
    }

    private void checkElementIndex(int index) {
        if (index < 0 || index >= size) {
            throw new RuntimeException("查找的位置不存在");
        }
    }

    @Override
    public T getValue(int index) {
        checkElementIndex(index);
        Node<T> p = first;
        for (int i = 0; i < index; i++) {
            p = p.next;
        }

        return p.data;
    }

    @Override
    public int size() {
        return size;
    }
}
